
package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.System.*;

public class BibliotecaApp {

    public static void main(String[] args) throws IOException{
        loginMessage();
        BibliotecaLibrary library=new BibliotecaLibrary();
        Scanner input = new Scanner(System.in);
        //BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String proceed="n";
        boolean authenticate = UserAuthentication(input);
        if(authenticate)
        {
            out.println("Enter Your Name: ");
            String customername = input.nextLine();
        do
        {
            authenticatedUserGetsTheseOptions(library, input, customername);
            System.out.println("Do u want to continue(y/n)?");
            proceed= input.nextLine();
        } while ( proceed.equals("y"));
        }
    }

    private static void authenticatedUserGetsTheseOptions(BibliotecaLibrary library, Scanner input, String customername) {
        welcomeMessage();
        ShowUserOptions();
        String choice= String.valueOf(UserSelectedOption());
        SelectedUserOption(library, input, choice, customername );
    }

    public static String welcomeMessage()
    {
        String msg = "WELCOME TO BIBLIOTECA";
        System.out.println("======================\n" + msg + "\n======================" );
        return msg;
    }

    public static String loginMessage()
    {
        String msg = "BIBLIOTECA LOGIN";
        System.out.println(msg);
        return msg;
    }

    public static  String ShowUserOptions()
    {
        String useroptions = ("1. List Books \n2. Checkout Book\n3. Return Book \n" +
                "4. List Movies \n" +
                "5. Checkout Movie \n6. Show My Details");
        System.out.println(useroptions);
        return useroptions;
    }

    public static String UserSelectedOption(){
        System.out.println("Enter Your Choice:");
        String choice = "1";
        Scanner input = new Scanner(System.in);
        choice = input.nextLine();
//        try {
//            choice = (char) System.in.read();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return choice;
    }

    private static void SelectedUserOption(BibliotecaLibrary library, Scanner input, String choice, String customername) {
        String bookname;
        String moviename;

        if (choice.equals("1")) {
            library.displayBook();
        }
        else if (choice.equals("2")) {
            out.println("Enter the book name to be checkedout");
            bookname = input.nextLine();
            library.checkOutBooks(bookname, customername);
            //BookSelectedByUser(library, bookname, customername);

        }
        else if (choice.equals("3")) {
            out.println("Enter the book name to be returned");
            bookname = input.nextLine();
            library.BookReturn(bookname);
//            BookSelectedByUserForReturn(library, bookname);
        }
        else if (choice.equals("4")) {
            library.displayMovies();
        }
        else if (choice.equals("5")) {
            out.println("Enter the movie name to be checkedout");
            moviename = input.nextLine();
            library.checkOutMovies(moviename);
//            MovieSelectedByUser(library, moviename);
        }
        else if (choice.equals("6")) {
            library.showUserDetails(customername);
        }
        else {
            out.println("Select a valid option!");
        }
    }

    private static boolean UserAuthentication(Scanner input) {
        String librarynumber;
        String password;
        Librarian librarian = new Librarian();
//        Customer customer1 = new Customer("vishwas", "vishwas@gmail.com","8123244329","123-xxxx","vishw@s");
//        Customer customer2 = new Customer("Kohli", "kohli@gmail.com","9686122334","124-xxxx","vishw@s");
//        Customer customer3 = new Customer("Gayle", "gayle@gmail.com","9797123467","125-xxxx","vishw@s");
//        List<Customer> customerlist = new ArrayList<Customer>();
//        customerlist.addAll(Arrays.asList(customer1, customer2, customer3));
        Register register = new Register();
        out.println("Enter Library Number: ");
        librarynumber = String.valueOf(input.nextLine().toCharArray());
        out.println("Enter Password Number: ");
        password = String.valueOf(input.nextLine().toCharArray());
        for(Customer list : register.customerlist)
        {
        if(librarynumber.equals(list.getLibrarynumber()) && password.equals(list.getPassword()))
        {

            return true;
        }
        }
        if(librarynumber.equals(librarian.getLibrariannumber()) && password.equals((librarian.getLibrarianpassword())))
        {
            String proceed="n";
           do{
               librarian.ShowLibrarianOptionsAndTakeInput();
               System.out.println("Do you want to continue(y/n)");
               proceed= input.nextLine();
               }while (proceed.equals("y"));
            return false;
           }

        else
        {
        out.println("INVALID USERNAME OR PASSWORD!!!!");
        return false;
        }
    }




//    private static void BookSelectedByUser(BibliotecaLibrary library, String bookname, String customername) {
//
//        Book book1 = new Book("The Hobbit", null);
//        Book book2 = new Book("Harry Potter", null);
//        Book book3 = new Book("Castle", null);
//        if (bookname.equalsIgnoreCase("The Hobbit")) {
//            book1.setCustomerborrowed(customername);
//            library.checkOutBooks(book1);
//        }
//        else if (bookname.equals("Harry Potter")) {
//            book2.setCustomerborrowed(customername);
//            library.checkOutBooks(book2);
//        }
//        else if (bookname.equals("Castle")) {
//            book3.setCustomerborrowed(customername);
//            library.checkOutBooks(book3);
//        }
//        else{
//            library.checkOutBooks(new Book());
//        }
//    }

//    private static void BookSelectedByUserForReturn(BibliotecaLibrary library, String bookname) {
//
//        Book book1 = new Book("The Hobbit", null);
//        Book book2 = new Book("Harry Potter", null);
//        Book book3 = new Book("Castle", null);
//        if (bookname.equals("The Hobbit")) {
//            library.BookReturn(book1);
//        }
//        else if (bookname.equals("Harry Potter")) {
//            library.BookReturn(book2);
//        }
//        else if (bookname.equals("Castle")) {
//            library.BookReturn(book3);
//        }
//        else{
//            library.BookReturn(new Book());
//        }
//    }

//    private static void MovieSelectedByUser(BibliotecaLibrary library, String moviename) {
//
//        Movies movie1 = new Movies("Robocop", "José Padilha" , 2014, 6);
//        Movies movie2 = new Movies("Spider-man", "Sam Raimi" , 2006, 8);
//        Movies movie3 = new Movies("Man Of Steel", "Zack Snyder" , 2012, 8);
//        if (moviename.equals("Robocop")) {
//            library.checkOutMovies(movie1);
//        }
//        else if (moviename.equals("Spider-man")) {
//            library.checkOutMovies(movie2);
//        }
//        else if (moviename.equals("Man Of Steel")) {
//            library.checkOutMovies(movie3);
//        }
//        else{
//            library.checkOutMovies(new Movies());
//        }
//    }
}
